package com.dom.domweb.dao.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dom.common.constants.ConstantError;
import com.dom.common.enums.RoleStatEnum;
import com.dom.common.exception.ServiceException;
import com.dom.common.model.vo.basic.*;
import com.dom.common.utils.BeanUtil;
import com.dom.domweb.dao.entity.TOrganInfo;
import com.dom.domweb.dao.entity.TRoleInfo;
import com.dom.domweb.dao.entity.TSysCodeTbl;
import com.dom.domweb.dao.mapper.TRoleInfoMapper;
import com.dom.domweb.dao.service.ITRoleInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dom.domweb.modular.shiro.model.ShiroUser;
import com.xiaoleilu.hutool.util.ObjectUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * B_角色信息表 服务实现类
 * </p>
 *
 * @author xllei
 * @since 2018-11-17
 */
@Service
public class TRoleInfoServiceImpl extends ServiceImpl<TRoleInfoMapper, TRoleInfo> implements ITRoleInfoService {
    @Override
    public List<TRoleInfo> findByUserId(int userId) {
        Wrapper<TRoleInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("id", userId);
        return selectList(wrapper);
    }

    @Override
    public List<TRoleInfo> findRoleList(int memCode) {
        Wrapper<TRoleInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("mem_code", memCode)
                .orderBy("id", true);
        List<TRoleInfo> roleInfos = this.selectList(wrapper);
        return roleInfos;
    }

    @Override
    public B080A addRoleInfo(B080R request) {
        TRoleInfo roleInfo = new TRoleInfo();
        BeanUtil.copyPropertiesIgnoreNull(request, roleInfo);
        roleInfo.setStat(RoleStatEnum.STAT_NORMAL.getCode());
        boolean result = this.insert(roleInfo);

        B080A response = new B080A();
        response.setRoleId(roleInfo.getId());
        return response;
    }

    @Override
    public B082A editRoleInfo(B082R request) {
        TRoleInfo roleInfo = this.selectById(request.getRoleId());

        if(ObjectUtil.isNotNull(request.getRoleName())){
            roleInfo.setRoleName(request.getRoleName());
        }
        if (ObjectUtil.isNotNull(request.getStat())) {
            roleInfo.setStat(request.getStat());
        }

        boolean result = this.updateById(roleInfo);

        B082A response = new B082A();
        response.setRoleId(roleInfo.getId());
        return response;
    }

    @Override
    public B081A delRoleInfo(B081R request) {
        TRoleInfo roleInfo = this.selectById(request.getRoleId());
        if (ObjectUtil.isNull(roleInfo)) {
            throw new ServiceException(ConstantError.NO_ORGAN_ERROR);
        }
        roleInfo.setStat(RoleStatEnum.STAT_STOP.getCode());

        this.updateById(roleInfo);
        B081A response = new B081A();
        response.setRoleId(roleInfo.getId());
        return response;
    }

    @Override
    public B089A findRoleByPager(B089R request, ShiroUser shiroUser) {
        Page<TRoleInfo> page = new Page<>();
        page.setCurrent(request.getPn());
        page.setSize(request.getPs());

        Wrapper<TRoleInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("mem_code", shiroUser.getMemCode());
        if(StringUtils.isNotEmpty(request.getRoleName())){
            wrapper.and().like("role_name", request.getRoleName());
        }
        if(ObjectUtil.isNotNull(request.getStat())){
            wrapper.and().eq("stat", request.getStat());
        }
        wrapper.orderBy("id", true);
        page = this.selectPage(page, wrapper);

        B089A response = new B089A();
        response.setTotalNum((int) page.getTotal());

        List<RoleInfo> roleInfos = new ArrayList<>();
        page.getRecords().forEach(entity -> {
            RoleInfo roleInfo = new RoleInfo();
            BeanUtil.copyPropertiesIgnoreNull(entity, roleInfo);
            roleInfo.setRoleId(entity.getId());
            roleInfos.add(roleInfo);
        });
        response.setRoleInfoData(roleInfos);
        return response;
    }
}
